package com.health.fragment.tijian;

import java.util.List;

import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.PersonalFileActivity.onFragmentSave;
import com.health.R;
import com.health.adapter.CheckBoxAdapter;
import com.health.adapter.RadioButtonAdapter;
import com.health.db.FDbManager;
import com.health.fragment.NovaFragment;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.ARCHIVE_HEALTHQUESTION;
import com.health.tablebean.BD_DD_INFO;
import com.health.viewscontrols.tijian.TiJianActivity;
import com.health.widget.MyGridView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

/**
 * 健康体检--健康问题
 * 
 * @author nova
 *
 */
public class HealthTiJian_HealthquesFragment extends NovaFragment implements onFragmentSave {
	// //脑血管疾病 PARENTID='DM100-26'
	// //肾脏疾病 PARENTID='DM100-27'
	// //心脏疾病 PARENTID='DM100-28'
	// //血管疾病 PARENTID='DM100-29'
	// //眼部疾病 PARENTID='DM100-30'
	// 神经系统 PARENTID='DM100-31'

	private MyGridView gd_nxg;// 脑血管疾病
	private MyGridView gd_sz;// 肾脏疾病
	private MyGridView gd_xz;// 心脏疾病
	private MyGridView gd_xg;// 血管疾病
	private MyGridView gd_ybjb;// 眼部疾病
	private MyGridView gd_sjxt;// 神经系统
	private MyGridView gd_qtxtjb;// 其他神经疾病f
	private RadioButtonAdapter adapter_sjxt = null;
	private RadioButtonAdapter adapter_qtxtjb = null;

	private CheckBoxAdapter adapter_nxg = null;
	private CheckBoxAdapter adapter_sz = null;
	private CheckBoxAdapter adapter_xz = null;
	private CheckBoxAdapter adapter_xg = null;
	private CheckBoxAdapter adapter_ybjb = null;

	private View healthQuesView = null;
	private FDbManager fdbmanger = null;
	private String str_sjxtId = "";
	private String str_sjxtContent = "";
	private String str_qtsjxtId = "";
	private String str_qtsjxtContent = "";

	private String str_nxgId = "";
	private String str_nxgContent = "";
	private String str_szId = "";
	private String str_szContent = "";
	private String str_xzId = "";
	private String str_xzContent = "";
	private String str_xgId = "";
	private String str_xgContent = "";
	private String str_ybjbId = "";
	private String str_ybjbContent = "";

	private int from = -1;// from ==1 从基本信息数据库处理 from ==0 第一次保存

	private ARCHIVE_BASEINFO userInfo;// 基本信息

	@Override
	public View findViews(LayoutInflater inflater) {
		fdbmanger = NovaApplication.fdbmanger;
		userInfo = NovaApplication.getUserInfo();
		healthQuesView = inflater.inflate(R.layout.healthtijian_healthques_fragment, null);
		initViews(healthQuesView);
		// GlobalConts.setMaxLength(length, editTexts);//TODO 所有其他都是50个字符

		return healthQuesView;
	}

	@Override
	public void setupView(View view) {

	}

	@Override
	public void addListener() {

	}

	@Override
	public void setData() {
		// //脑血管疾病 PARENTID='DM100-26'
		// //肾脏疾病 PARENTID='DM100-27'
		// //心脏疾病 PARENTID='DM100-28'
		// //血管疾病 PARENTID='DM100-29'
		// //眼部疾病 PARENTID='DM100-30'
		// 神经系统 PARENTID='DM100-31'
		bindDdata("脑血管疾病", "PARENTID='DM100-26'");
		bindDdata("肾脏疾病", "PARENTID='DM100-27'");
		bindDdata("心脏疾病", "PARENTID='DM100-28'");
		bindDdata("血管疾病", "PARENTID='DM100-29'");
		bindDdata("眼部疾病", "PARENTID='DM100-30'");
		bindDdata("神经系统", "PARENTID='DM100-31'");
		bindDdata("其他神经系统", "PARENTID='DM100-31'");
		setShowData();

	}
	
	@Override
	public void onResume() {
		super.onResume();
//		setData();
	}

	/**
	 * 初始化布局
	 * 
	 * @param v
	 * @author 王婷婷
	 * @date 2015年12月17日 上午10:13:55
	 */
	private void initViews(View v) {
		gd_nxg = (MyGridView) v.findViewById(R.id.gd_nxg);
		gd_sz = (MyGridView) v.findViewById(R.id.gd_sz);
		gd_xz = (MyGridView) v.findViewById(R.id.gd_xz);
		gd_xg = (MyGridView) v.findViewById(R.id.gd_xg);
		gd_ybjb = (MyGridView) v.findViewById(R.id.gd_ybjb);
		gd_sjxt = (MyGridView) v.findViewById(R.id.gd_sjxt);
		gd_qtxtjb = (MyGridView) v.findViewById(R.id.gd_qtxtjb);
	}

	/**
	 * 绑定数据值
	 * 
	 * @param str_where
	 * @param flag
	 * @author 王婷婷
	 * @date 2015年12月17日 上午10:30:30
	 */
	private void bindDdata(String flag, String str_where) {
		List<BD_DD_INFO> listZZs = fdbmanger.findAllByWhereInInfoDB(BD_DD_INFO.class, str_where);
		// //脑血管疾病 PARENTID='DM100-26'
		// //肾脏疾病 PARENTID='DM100-27'
		// //心脏疾病 PARENTID='DM100-28'
		// //血管疾病 PARENTID='DM100-29'
		// //眼部疾病 PARENTID='DM100-30'
		// 神经系统 PARENTID='DM100-31'

		if (flag.equals("脑血管疾病")) {

			adapter_nxg = new CheckBoxAdapter(getActivity(), listZZs);
			adapter_nxg.setOtherMaxLength(50);
			gd_nxg.setAdapter(adapter_nxg);
		} else if (flag.equals("肾脏疾病")) {

			adapter_sz = new CheckBoxAdapter(getActivity(), listZZs);
			adapter_sz.setOtherMaxLength(50);
			gd_sz.setAdapter(adapter_sz);
		} else if (flag.equals("心脏疾病")) {

			adapter_xz = new CheckBoxAdapter(getActivity(), listZZs);
			adapter_xz.setOtherMaxLength(50);
			gd_xz.setAdapter(adapter_xz);
		} else if (flag.equals("血管疾病")) {

			adapter_xg = new CheckBoxAdapter(getActivity(), listZZs);
			adapter_xg.setOtherMaxLength(50);
			gd_xg.setAdapter(adapter_xg);
		} else if (flag.equals("眼部疾病")) {

			adapter_ybjb = new CheckBoxAdapter(getActivity(), listZZs);
			adapter_ybjb.setOtherMaxLength(50);
			gd_ybjb.setAdapter(adapter_ybjb);
		} else if (flag.equals("神经系统")) {

			adapter_sjxt = new RadioButtonAdapter(listZZs, getActivity());
			gd_sjxt.setAdapter(adapter_sjxt);
		} else if (flag.equals("其他神经系统")) {
			adapter_qtxtjb = new RadioButtonAdapter(listZZs, getActivity());
			gd_qtxtjb.setAdapter(adapter_qtxtjb);
		}

	}

	/**
	 * 获取转诊的信息
	 * 
	 * @author 王婷婷
	 * @date 2015年12月16日 下午3:10:48
	 */
	private void getRadioButtonCheckedData() {
		// 获取神经系统数据
		str_sjxtId = adapter_sjxt.getChecked();
		str_sjxtContent = adapter_sjxt.getCheckedOther();
		str_qtsjxtId = adapter_qtxtjb.getChecked();
		str_qtsjxtContent = adapter_qtxtjb.getCheckedOther();
	}

	/**
	 * 获取 被选中的checkbox
	 * 
	 * @author 王婷婷
	 * @date 2015年12月19日 上午11:34:30
	 */
	private void getCheckBoxedData() {

		str_nxgId = adapter_nxg.getCheckedIdsStr();// TODO
		str_nxgContent = adapter_nxg.getOther();
		str_szId = adapter_sz.getCheckedIdsStr();
		str_szContent = adapter_sz.getOther();
		str_xzId = adapter_xz.getCheckedIdsStr();
		str_xzContent = adapter_xz.getOther();
		str_xgId = adapter_xg.getCheckedIdsStr();
		str_xgContent = adapter_xg.getOther();
		str_ybjbId = adapter_ybjb.getCheckedIdsStr();
		str_ybjbContent = adapter_ybjb.getOther();

	}

	/**
	 * 保存数据
	 * 
	 * @author 王婷婷
	 * @date 2015年12月17日 上午11:13:39
	 */
	private void saveData() {

//		if (TextUtils.isEmpty(adapter_nxg.getCheckedIdsStr())) {
//			((TiJianActivity) getActivity()).showTextDialog("请填写脑血管疾病");
//			return;
//		}
//
//		if (TextUtils.isEmpty(adapter_sz.getCheckedIdsStr())) {
//			((TiJianActivity) getActivity()).showTextDialog("请填写肾脏疾病");
//			return;
//		}
//		if (TextUtils.isEmpty(adapter_xz.getCheckedIdsStr())) {
//			((TiJianActivity) getActivity()).showTextDialog("请填写心脏疾病");
//			return;
//		}
//		if (TextUtils.isEmpty(adapter_xg.getCheckedIdsStr())) {
//			((TiJianActivity) getActivity()).showTextDialog("请填写血管疾病");
//			return;
//		}
//		if (TextUtils.isEmpty(adapter_ybjb.getCheckedIdsStr())) {
//			((TiJianActivity) getActivity()).showTextDialog("请填写眼部疾病");
//			return;
//		}
//		if (TextUtils.isEmpty(adapter_sjxt.getChecked())) {
//			((TiJianActivity) getActivity()).showTextDialog("请填写神经系统");
//			return;
//		}
//		if (TextUtils.isEmpty(adapter_qtxtjb .getChecked())) {
//			((TiJianActivity) getActivity()).showTextDialog("请填写其他系统疾病");
//			return;
//		}

		ARCHIVE_HEALTHQUESTION ques = new ARCHIVE_HEALTHQUESTION();

		ques.setIDCARD(userInfo.getIDCARD());
		ques.setNERVE_DIS(str_sjxtId);
		ques.setELSE_DIS(str_qtsjxtId);
		ques.setEYE_DIS(str_ybjbId);// 眼部疾病
		ques.setVESSEL_DIS(str_xgId);// 血管疾病
		ques.setHEART_DIS(str_xzId);// 心脏疾病
		ques.setRENAL_DIS(str_szId);// 肾脏疾病
		String str = str_nxgId;
		System.out.println(str);
		ques.setBRAIN_DIS(str_nxgId);
		// TODO 脑血管疾病

		Log.w("eeeeeeeettttttttttt",
				str_ybjbId + "\n" + str_xgId + "\n" + str_xzId + "\n" + str_szId + "\n" + str_nxgId);// TODO
		ques.setPHYSICALID("");// 体检编号
		ques.setBRAIN_OTHER(str_nxgContent);// 脑血管疾病--其他
		ques.setRENAL_OTHER(str_szContent);// 肾脏疾病--其他
		ques.setHEART_OTHER(str_xzContent);// 心脏疾病-其他
		ques.setVESSEL_OTHER(str_xgContent);// 血管疾病--其他

		ques.setEYE_OTHER(str_ybjbContent);// 眼部疾病--其他
		ques.setNERVE_DIS_OTHER(str_sjxtContent);// 神经系统疾病--其他
		ques.setELSE_DIS_OTHER(str_qtsjxtContent);// 其他系统疾病--其它
		ques.setRENAL_OTHER(str_szContent);// 肾脏疾病--其他
		ques.setHEART_OTHER(str_xzContent);// 心脏疾病-其他
		if (from == 1) {
			fdbmanger.updata(ques, "IDCARD='" + userInfo.getIDCARD() + "'");
		} else {
			fdbmanger.save(ques);
			from = 1;
		}
		((TiJianActivity) getActivity()).showTextDialog("保存成功");

	}

	/**
	 * 设置展示的信息
	 * 
	 * @author 王婷婷
	 * @date 2015年12月17日 上午11:31:24
	 */
	private void setShowData() {
		List<ARCHIVE_HEALTHQUESTION> queses = fdbmanger.findAllByWhere(ARCHIVE_HEALTHQUESTION.class,
				"IDCARD='" + userInfo.getIDCARD() + "'");
		if (queses.size() > 0) {
			from = 1;
			ARCHIVE_HEALTHQUESTION ques = queses.get(0);
			str_sjxtId = ques.getNERVE_DIS();
			str_sjxtContent = ques.getNERVE_DIS_OTHER();

			str_qtsjxtId = ques.getELSE_DIS();
			str_qtsjxtContent = ques.getELSE_DIS_OTHER();

			adapter_sjxt.setChecked(str_sjxtId, str_sjxtContent);
			adapter_qtxtjb.setChecked(str_qtsjxtId, str_qtsjxtContent);
			Log.w("eeeeshowdata-------------",
					str_sjxtId + "\n" + str_sjxtContent + "\n" + str_qtsjxtId + "\n" + str_qtsjxtContent);

			str_nxgId = ques.getBRAIN_DIS();// TODO
			str_nxgContent = ques.getBRAIN_OTHER();
			adapter_nxg.setChecked(str_nxgId);// TODO
			adapter_nxg.setOther(str_nxgContent);
			str_szId = ques.getRENAL_DIS();
			str_szContent = ques.getRENAL_OTHER();
			adapter_sz.setChecked(str_szId);
			adapter_sz.setOther(str_szContent);
			str_xzId = ques.getHEART_DIS();
			str_xzContent = ques.getHEART_OTHER();
			adapter_xz.setChecked(str_xzId);
			adapter_xz.setOther(str_xzContent);
			str_xgId = ques.getVESSEL_DIS();
			str_xgContent = ques.getVESSEL_OTHER();
			adapter_xg.setChecked(str_xgId);
			adapter_xg.setOther(str_xgContent);
			str_ybjbId = ques.getEYE_DIS();
			str_ybjbContent = ques.getEYE_OTHER();
			adapter_ybjb.setChecked(str_ybjbId);
			adapter_ybjb.setOther(str_ybjbContent);

		} else {
			from = 0;
		}

	}

	@Override
	public void save() {
		getRadioButtonCheckedData();
		getCheckBoxedData();
		saveData();

	}
}
